package com.keqiang.chart.impl.axis.impl.bar.horizontal

import android.graphics.Canvas
import androidx.annotation.FloatRange
import androidx.annotation.Px
import com.keqiang.chart.base.BaseChartView
import com.keqiang.chart.impl.axis.AxisEntity
import com.keqiang.chart.impl.axis.BaseAxisChart
import com.keqiang.chart.impl.axis.BaseTouchHandler
import com.keqiang.chart.impl.axis.BaseYAxis
import com.keqiang.chart.impl.axis.XAxis
import com.keqiang.chart.impl.axis.XAxisHorizontal
import com.keqiang.chart.impl.axis.YAxis
import com.keqiang.chart.impl.axis.YAxisHorizontal
import com.keqiang.chart.impl.axis.impl.bar.BarEntity
import com.keqiang.chart.impl.axis.render.BaseDataRender
import com.keqiang.chart.impl.axis.render.XAxisRender
import com.keqiang.chart.impl.axis.render.YAxisRender
import com.keqiang.chart.utils.px

/**
 * 横向柱状图
 *
 * @author Created by wanggaowan on 2022/7/29 15:15
 */
open class BarChartHorizontal(view: BaseChartView<*>) : BaseAxisChart<BarData>(view) {
    override var xAxis: XAxisHorizontal = XAxisHorizontal()
    override var yAxis: YAxisHorizontal = YAxisHorizontal()
    override var yAxisRender: YAxisRender<BarChartHorizontal> = YAxisRender(this, false)
    override var xAxisRender: XAxisRender<BarChartHorizontal> = XAxisRender(this)
    override var dataRender: BaseDataRender<BarChartHorizontal> = BarChartHorizontalDataRender(this)
    override var touchHandler: BaseTouchHandler<BarChartHorizontal, List<BarEntity>>? =
        BarHorizontalTouchHandler(this)

    var highLightRender: HighLightRender? = null

    override fun getGridHeight(yAxis: BaseYAxis, yAxisLength: Float): Float {
        return yAxisLength / yAxis.labelCount
    }

    override fun isEmptyData(): Boolean {
        return data == null
    }
}

/**
 * 高亮数据绘制器
 *
 * @author Created by wanggaowan on 2022/11/1 10:07
 */
abstract class HighLightRender(val chart: BarChartHorizontal) {
    /**
     * 绘制高亮内容，此时[AxisEntity.drawRectF]已就绪
     *
     * @param canvas 画布
     * @param start  水平可绘制区域起始点坐标 = x轴起始点 + [XAxis.startPadding]
     * @param top    可绘制区域顶部坐标 = y轴顶部 - [YAxis.endPadding]
     * @param end    水平可绘制区域结束点坐标 = x轴结束点 - [XAxis.endPadding]
     * @param bottom 可绘制区域底部坐标 = y轴底部 + [YAxis.startPadding]
     */
    abstract fun onDraw(canvas: Canvas, start: Float, top: Float, end: Float, bottom: Float, data: List<BarEntity>)
}

/**
 * 柱状图数据
 */
open class BarData {
    var groups: List<BarGroup>? = null
}

/**
 * 柱状图数据
 */
open class BarGroup(
    /**
     * 需要绘制的值
     */
    var values: List<BarEntity>) {

    /**
     * 多个柱形图之间的间隔
     */
    @Px
    @FloatRange(from = 0.0)
    var barOffset = 10f.px

    /**
     * 柱状图绘制方向，参考[GRAVITY_TOP]、[GRAVITY_CENTER]、[GRAVITY_BOTTOM]
     */
    var gravity: Int = GRAVITY_CENTER

    companion object {
        /**
         * 从网格顶部往下绘制
         */
        const val GRAVITY_TOP = 0

        /**
         * 从网格中间向两边绘制
         */
        const val GRAVITY_CENTER = 1

        /**
         * 从网格底部网上绘制
         */
        const val GRAVITY_BOTTOM = 2
    }
}
